var async = require('async');
var api = require('./api.js')
var Q = require('Q');
module.exports = {
    updateSignDayGive:function(uid){
        var q = Q.defer();

        async.waterfall([
            function(cb){
                api.getInfo().then(function(data){
                    cb(null,data);
                }).catch(function(err){
                    cb({code:-1});
                })
            },
            function(info,cb){
                async.eachSeries(info,function(each,cb1){
                    api.updateSignDayGive(each.uid).then(function(data){
                        console.log(each.uid);
                        if(data.code === 0){
                            cb1(null, {code:0})
                        }else{
                            cb1(null, {code:0})
                        }
                    }).catch(function(err){
                        cb1(null, {code:0})
                    });
                    /*console.log('=======')
                    //console.log(each)
                    console.log(each.uid);
                    cb1(null, {code:0});*/
                },function(){
                    cb(null, {code:0});
                })
            },
            function(user,cb){
                if(user.code===0){
                    cb(null, {code:0})
                }else{
                    cb({code:-1})
                }
            }
        ],function(err,results){
            if(err){
                q.reject(err);
            }else{
                q.resolve(results);
            }
        });
        return q.promise ;
    },
}